﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;


namespace TCDC_System.Content.MasterData
{
    public partial class p_tcdc_mst_campaign : SystemPage.UIPage
    {
        private tcdc_mst_campaign objCampaign;
        protected void Page_Init(object sender, EventArgs e)
        {
            this.CampaignAdd1.GridStore = StoreCampaign;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            objCampaign = new tcdc_mst_campaign();
            objCampaign.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StoreCampaign.DataSource = GetCampaign();
                StoreCampaign.DataBind();
            }
        }

        protected DataTable GetCampaign()
        {
            DataTable dt = objCampaign.getByObject(new tcdc_mst_campaign());
            return dt;
        }

        protected void AddCampaign(object sender, DirectEventArgs e)
        {
            CampaignAdd1.Show();
        }

        protected void UpdateCampaign(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["CampaignValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string campaign_id = string.Empty;
            string product_id = string.Empty;
            string member_type_id = string.Empty;
            string campaign_group_id = string.Empty;
            string campaign_name_th = string.Empty;
            string campaign_name_en = string.Empty;
            string add_year = string.Empty;
            string add_month = string.Empty;
            string price = string.Empty;
            string begin_date = string.Empty;
            string end_date = string.Empty;
            string promotion_code = string.Empty;
            string promotion_public = string.Empty;
            string site_id = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                campaign_id = row["campaign_id"].ToString();
                product_id = row["product_id"].ToString();
                member_type_id = row["member_type_id"].ToString();
                campaign_group_id = row["campaign_group_id"].ToString();
                campaign_name_th = row["campaign_name_th"].ToString();
                campaign_name_en = row["campaign_name_en"].ToString();
                add_year = row["add_year"].ToString();
                add_month = row["add_month"].ToString();
                price = row["price"].ToString();
                begin_date = row["begin_date"].ToString();
                end_date = row["end_date"].ToString();
                promotion_code = row["promotion_code"].ToString();
                promotion_public = row["promotion_public"].ToString();
                site_id = row["site_id"].ToString();
            }

            objCampaign.campaign_id = Convert.ToInt32(campaign_id);
            objCampaign.product_id = Convert.ToInt32(product_id);
            objCampaign.member_type_id = Convert.ToInt32(member_type_id);
            objCampaign.campaign_group_id = Convert.ToInt32(campaign_group_id);
            objCampaign.campaign_name_th = campaign_name_th;
            objCampaign.campaign_name_en = campaign_name_en;
            objCampaign.add_year = Convert.ToInt32(add_year);
            objCampaign.add_month = Convert.ToInt32(add_month);
            objCampaign.price = Convert.ToDouble(price);
            objCampaign.begin_date = begin_date;
            objCampaign.end_date = end_date;
            objCampaign.promotion_code = promotion_code;
            objCampaign.promotion_public = promotion_public;
            objCampaign.site_id = Convert.ToInt32(site_id);
            objCampaign.updateData(objCampaign);
            StoreCampaign.DataSource = GetCampaign();
            StoreCampaign.DataBind();
        }

        protected void DeleteCampaign(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteCampaign();
        }

        protected void StoreCampaign_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreCampaign.DataSource = GetCampaign();
            StoreCampaign.DataBind();
        }

        protected void ExportExcelCampaign(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=Campaign.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteCampaign()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataCampaign()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataCampaign()
        {
            RowSelectionModel sm = this.GridPanelCampaign.SelectionModel.Primary as RowSelectionModel;
            string campaign_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                campaign_id = row.RecordID;
            }
            objCampaign.deleteData(campaign_id);
            StoreCampaign.DataSource = GetCampaign();
            StoreCampaign.DataBind();
        }
    }
}

